const Header = {
    "Content-Type": "application/json; charset=UTF-8",
    "Accept": "application/json; version=2.0.0",
};

const request = {
    post: async (url, params, successCallback, failureCallback) => {
        try {
            let res = await fetch(url, {
                method: 'POST',
                header: Header,
                body: params,
            });
            let response = await res.json();
            successCallback(response);
        } catch (error) {
            failureCallback(error);
        }
    },
    get: async (url, params, successCallback, failureCallback) => {
        if (params) {
            let paramsArray = [];
            Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]));
            if (url.search(/\?/) === -1) {
                url += '?' + paramsArray.join('&');
            } else {
                url += '&' + paramsArray.join('&');
            }
        }

        try {
            let response = await
                fetch(url, {
                    method: 'GET',
                    header: Header,
                });
            let responseObject = await response.json();
            successCallback(responseObject);
        } catch (error) {
            failureCallback(error);
        }
    }
};

module.export = request;